<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
        list-style: none;
        border: 0;
      }
      .all {
        width: 500px;
        height: 200px;
        padding: 7px;
        border: 1px solid #ccc;
        margin: 100px auto;
        position: relative;
      }
      .screen {
        width: 500px;
        height: 200px;
        overflow: hidden;
        position: relative;
      }
      .screen li {
        width: 500px;
        height: 200px;
        overflow: hidden;
        float: left;
      }
      .screen ul {
        position: absolute;
        left: 0;
        top: 0px;
        width: 3000px;
      }
      .all ol {
        position: absolute;
        right: 10px;
        bottom: 10px;
        line-height: 20px;
        text-align: center;
      }
      .all ol li {
        float: left;
        width: 20px;
        height: 20px;
        background: #fff;
        border: 1px solid #ccc;
        margin-left: 10px;
        cursor: pointer;
      }
      .all ol li.active {
        background: rgb(108, 78, 33);
      }
      /*#arr {display: none;}*/
      #arr span {
        width: 40px;
        height: 40px;
        position: absolute;
        left: 5px;
        top: 50%;
        margin-top: -20px;
        background: #000;
        cursor: pointer;
        line-height: 40px;
        text-align: center;
        font-weight: bold;
        font-family: "黑体";
        font-size: 30px;
        color: #fff;
        opacity: 0.3;
        border: 1px solid #fff;
      }
      #arr #right {
        right: 5px;
        left: auto;
      }
    </style>
  </head>
  <body>
    <div class="all" id="all">
      <div class="screen" id="screen">
        <ul id="ul">
          <li><img src="./images/1.webp" width="500" height="200" /></li>

          <li><img src="./images/2.webp" width="500" height="200" /></li>

          <li><img src="./images/3.webp" width="500" height="200" /></li>

          <li><img src="./images/4.webp" width="500" height="200" /></li>

          <li><img src="./images/5.webp" width="500" height="200" /></li>
        </ul>

        <ol></ol>
        <div id="arr"><span id="left"><</span> <span id="right">></span></div>
      </div>
    </div>

    <script>
      window.onload = function () {
        var ul = document.querySelector("ul");
        var ol = document.querySelector("ol");
        var prev = document.querySelector("#left");
        var next = document.querySelector("#right");
        var all = document.querySelector("#all");
        var olIndex = 0;
        var imgIndex = 0;
        // 创建小圆点
        for (var i = 0; i < ul.children.length; i++) {
          var point = document.createElement("li");
          point.innerHTML = i + 1;

          ol.appendChild(point);
        }

        ol.children[olIndex].className = "active";
        prev.onclick = function () {
          olIndex--;
          imgIndex--;
          if (olIndex < 0) {
            ul.style.left = -(ul.children.length - 1) * 500 + "px";
            imgIndex = ul.children.length - 1;
            olIndex = ul.children.length - 1;
          }
          olIndex = olIndex < 0 ? ol.children.length - 1 : olIndex;
          // 图片切换
          // 容器，对象
          animate(ul, { left: -500 * imgIndex });
          light();
        };
        // 自动播放
        next.onclick = autoplay;
        var timer = setInterval(autoplay, 500);
        // 鼠标移入暂停，鼠标移出自动播放
        all.onmousemove = function () {
          clearInterval(timer);
        };
        all.onmouseleave = function () {
          clearInterval(timer);
          timer = setInterval(autoplay, 500);
        };
        function autoplay() {
          olIndex++;
          imgIndex++;
          if (olIndex > ul.children.length - 1) {
            ul.style.left = (ul.children.length - 5) * 500 + "px";
            imgIndex = ul.children.length - 5;
            olIndex = ul.children.length - 5;
          }
          olIndex = olIndex > ol.children.length - 1 ? 0 : olIndex;
          // 图片切换
          // 容器，对象
          animate(ul, { left: -500 * imgIndex });
          light();
        }
        // 排他思想
        function light() {
          for (var i = 0; i < ul.children.length; i++) {
            var item = ol.children[i];
            item.className = "";
          }

          ol.children[olIndex].className = "active";
        }

        // 返回指定dom节点的attr样式值
        function getStyle(dom, attr) {
          if (window.getComputedStyle) {
            // 是否有getComputedStyle方法
            //非IE低版
            return window.getComputedStyle(dom, null)[attr];
          } else {
            //IE低版
            return dom.currentStyle[attr];
          }
        }

        // 封装一个缓动的动画函数，需要时调用即可
        function animate(dom, json, callback) {
          /*
            dom:要运动的节点
            json:要运动的样式名
            callback:运动完成的回调函数
        */ //要用定时器，先清定时器 //要运动的定时器
          clearInterval(dom.timer);
          dom.timer = setInterval(function () {
            //json有几个属性，就要运动几次
            var flag = true;
            for (var attr in json) {
              //获取当前位置点击right按钮，看下一张，点亮小圆点
              // 计算速度
              var current = parseInt(getStyle(dom, attr));
              var speed =
                json[attr] - current > 0
                  ? Math.ceil((json[attr] - current) / 10)
                  : Math.floor((json[attr] - current) / 10); //计算下一个位置
              //定位元素
              var next = current + speed;
              //判断是否达到目标
              dom.style[attr] = next + "px";
              if (next != json[attr]) {
                flag = false;
              }
            }
            if (flag) {
              clearInterval(dom.timer);
              //为了防止造成不传造成当前函数调用的报错
              callback && callback();
            }
          }, 20); // 每20毫秒
        }
      };
    </script>
  </body>
</html>
